﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestGenerator
{
   public class Question
    {
        static Random rand = new Random();
        string text;
        List<string> WrongAnswer;
        List<string> CorrectAnswer;
        
        public Question(string text, List<string> WA, List<string> CA)
        {
            this.text = text;
            WrongAnswer = WA;
            CorrectAnswer = CA;
        }

        public bool checkForEqual(int[] array) 
        {
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] != -1)
                {
                    return false;
                }
            }
            return true;
        }
        public string ToString(Settings settings , int number, out char indexOfCorrectAnswer)
        {
            indexOfCorrectAnswer = 'a';
            string result = "<p class=Question>";
            result = result + number + "." + text + "</p>\n";
            int positionOfCorrectAnswer = rand.Next(CorrectAnswer.Count);
            int[] taken = new int[WrongAnswer.Count];
            for (int i = 0;i < settings.MaxAnswersPerQuestion;i++)
            {
                if (i != positionOfCorrectAnswer)
                {
                    if (checkForEqual(taken))
                        break;
                    int index = rand.Next( WrongAnswer.Count);
                    while (taken[index] == -1)
                    {
                        index = rand.Next(WrongAnswer.Count);
                    }
                    taken[index] = -1;
                    result = result + "<p class=Answer>" + ((char)('а'+i))+ ")" + WrongAnswer[index] + "</p>" + "\n";
                }
                else 
                {
                    int indexOfCorrect = rand.Next(0,CorrectAnswer.Count);
                    result = result + "<p class=Answer>" + ((char)('а' + i)) + ")" + CorrectAnswer[indexOfCorrect] + "</p>" + "\n";
                    indexOfCorrectAnswer = (char)('а' + i);
                }
            }
            return result;
        }
    }
}
